package region

import (
	"context"
	"fast-boot/app/model"
	"fast-boot/common/xerr"

	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改菜单
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req *types.SysRegionForm) (resp *types.SysRegionFormResp, err error) {
	info := model.SysRegionModel{}
	l.svcCtx.GormClient.GormDb.First(&info, req.Id)
	if info.Id == 0 {
		return nil, xerr.NewErrMsg("不存在")
	}
	info.RegionCode = req.RegionCode
	info.ParentCode = req.ParentCode
	info.RegionName = req.RegionName

	if err := l.svcCtx.GormClient.GormDb.Save(&info).Error; err != nil {
		return nil, err
	}
	return &types.SysRegionFormResp{Id: info.Id}, nil

}
